package string

import (
	"crypto/md5"
	"encoding/hex"
	"encoding/json"
	"strconv"
	"time"
)

func StringToTime(strTime string) (*time.Time, error) {
	const TIME_LAYOUT = "2006-01-02 15:04:05" //此时间不可更改
	timeobj, err := time.ParseInLocation(TIME_LAYOUT, strTime, time.Local)
	return &timeobj, err
}

func StructToJson(data interface{}) string {
	b, _ := json.Marshal(data)
	return string(b)
}

func StringToFloat64(str string) float64 {
	v, _ := strconv.ParseFloat(str, 64)
	return v
}

func MD5(text string) string {
	ctx := md5.New()
	ctx.Write([]byte(text))
	return hex.EncodeToString(ctx.Sum(nil))
}

func If(n bool, n2 interface{}, n3 interface{}) interface{} {
	if n {
		return n2
	} else {
		return n3
	}
}

func UnixToTime(e string) (datatime time.Time, err error) {
	data, err := strconv.ParseInt(e, 10, 64)
	datatime = time.Unix(data/1000, 0)
	return
}

func TimeToUnix(e time.Time) int64 {
	timeUnix, _ := time.Parse("2006-01-02 15:04:05", e.Format("2006-01-02 15:04:05"))
	return timeUnix.UnixNano() / 1e6
}
